<?php
require 'HttpsRequest.php';
require 'CodeImg.php';


//获取accesstoken
 $appid = "wxc2ce9a7fdf838194"; 
 $appsecret = "48b9e13cab338bc7a9be9a4479c27074";
 $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
 
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 $output = curl_exec($ch);
 curl_close($ch);
 $info = json_decode($output, true);
 var_dump($info);


//保存accesstoken到数据库
$link = mysqli_connect('localhost', 'root', '');
mysqli_query($link,"SET NAMES 'UTF8'");
mysqli_select_db($link,"tpshop2.0");

$in = $info['access_token'];
$con=mysqli_connect("localhost","root","","tpshop2.0"); 
if (mysqli_connect_errno($con)) 
{ 
    echo "连接 MySQL 失败: " . mysqli_connect_error(); 
} 
mysqli_query($con,"INSERT INTO 101_token (accesstoken)
VALUES ('$in')");
mysqli_close($con);

echo $in;

//生成二维码链接
// $CodeImg = new CodeImg;
// $CodeImg->setId(1500);//二维码场景ID
// $CodeImg->setAccessToken($info['access_token']);
// echo $CodeImg->getCodeUrl();
// //需要取出ticket或url
// $url=$CodeImg->getCodeUrl();
//跳转二维码页面
//header( "Location: $url");
//ticket
//$CodeImg->getTicket(),'<br/>',



   //获取关注公众号用户的openid
 //  $access_token = $info['access_token'];
 //  $url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=$access_token";
 //  $result = https_re($url);
  
 //  function https_re($url, $data = null)
 //  {
 //     $curl = curl_init();
 //     curl_setopt($curl, CURLOPT_URL, $url);
 //     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
 //     curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
 //     if (!empty($data)){
 //         curl_setopt($curl, CURLOPT_POST, 1);
 //         curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
 //     }
 //     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 //     $out = curl_exec($curl);
 //     curl_close($curl);
 //     //json转换数组
 //     var_dump(json_decode($out, true));
 //     //返回json格式
 //     return $out;
 // }


 // $access_token = $info['access_token'];
 // $openid =openid;
 // $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN";
 // $output = https_request($url);
 // var_dump($output);
 
 // function https_request($url, $data = null)
 // {
 //     $curl = curl_init();
 //     curl_setopt($curl, CURLOPT_URL, $url);
 //     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
 //     curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
 //     if (!empty($data)){
 //         curl_setopt($curl, CURLOPT_POST, 1);
 //         curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
 //     }
 //     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 //     $output = curl_exec($curl);
 //     curl_close($curl);
 //     return $output;
 // }



